package com.ys.service.storage.impl;

import com.alibaba.fastjson.JSON;
import com.ys.dto.AddDto;
import com.ys.dto.EditDto;
import com.ys.entity.base.TbUserJobLevelManage;
import com.ys.entity.business.TpProductActive;
import com.ys.entity.storage.TccWarehouse;
import com.ys.mapper.storage.TccWarehouseMapper;
import com.ys.service.storage.TccWarehouseService;
import com.ys.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 仓库管理实现层
 */
@Service("tccWarehouseService")
public class TccWarehouseServiceImpl implements TccWarehouseService {
    private static final Logger logger = LoggerFactory.getLogger(TccWarehouseServiceImpl.class);


    @Autowired
    private TccWarehouseMapper tccWarehouseMapper;

    /**
     * 新增
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public R add(Map<String, Object> params) throws Exception {
        String countryCode = params.get("countryCode").toString();
        String vck = params.get("vck").toString();
        if (countryCode == null || vck == null) {
            return R.error("请补充必填项");
        } else if (new TccWarehouse().setVck(vck).setCountryCode(countryCode).count() > 0 ) {
            return R.error("不符合仓库名称-国家唯一性");
        }
        TccWarehouse entity = JSON.parseObject(JSON.toJSONString(params), TccWarehouse.class);

        tccWarehouseMapper.insert(entity);
        return R.ok(AddDto.closeAndReload());
    }
    @Override
    @Transactional
    public R edit(Map<String, Object> params) throws Exception {
        // 验证编码
        String countryCode = params.get("countryCode").toString();
        String vck = params.get("vck").toString();
        if (new TccWarehouse().setVck(vck).setCountryCode(countryCode).where("A.Id != '" + params.get("id").toString() + "'").count() > 0) {
            return R.error("不符合仓库名称-国家唯一性");
        }
        TccWarehouse entity = JSON.parseObject(JSON.toJSONString(params), TccWarehouse.class);
        tccWarehouseMapper.update(entity);
        return R.ok("操作成功");
    }
}
